﻿<%@ Page Language="VB" %>

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    
    Public strMessage As String
    Dim strQueryOrdering As String = " ORDER BY Persons.Name "
   
    Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        ' Setting of default DB table
        Session("TableName") = "Persons"
        Dim sC1 As String
        Dim sWhere As String = ""
        
        '	Authorization level 4 needed for this function
        If Int32.Parse(Session("userLevel")) < 4 Then
            Response.Redirect("/timereport/menu.aspx?msgtype=E&msgno=" & ConfigurationSettings.AppSettings("MSGNO_AUTHORIZATION_FAILED"))
        End If
        
        ' Imposta il SelectCommand in base al contenuto della lista dropdown
        If DL_flattivo.SelectedValue <> "all" Or _
          (Session("Persons_DL_flattivo_val") <> Nothing And Not IsPostBack) Then
            sWhere = " WHERE Persons.Active IN (@DL_flattivo)"
        End If
        
        If TB_Nome.Text <> Nothing Or _
            (Session("TB_Nome_val") <> Nothing And Not IsPostBack) Then
            sC1 = IIf(sWhere = "", " WHERE ", " AND ")
            sWhere = sWhere & sC1 & "Persons.Name LIKE '%' + (@TB_Nome) + '%' "
        End If
        
        PersonsLookUpSqlDataSource.SelectCommand = "SELECT Persons.Persons_id, Persons.Name as Nome, Roles.Name as Ruolo FROM Persons INNER JOIN Roles ON Persons.Roles_id = Roles.Roles_Id" & sWhere & strQueryOrdering
                
    End Sub
            
    Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        'Response.Redirect("projects_lookup_form.aspx?Projectcode=" & GridView1.SelectedValue)
        Response.Redirect("/timereport/lookup_detail.asp?action=update&Id=" & GridView1.SelectedValue)
    End Sub

    Protected Sub DL_flattivo_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Session("Persons_DL_flattivo_val") = IIf(sender.selectedValue <> "all", sender.selectedValue, Nothing)
    End Sub
    
    Protected Sub TB_Codice_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Session("TB_Nome_val") = sender.text
    End Sub
        
    Protected Sub TB_Nome_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        ' Resetta valore textbox per non perderlo a seguito passaggio a pagina di dettaglio
        If Not IsPostBack And Session("TB_Nome_val") <> Nothing Then
            TB_Nome.Text = Session("TB_Nome_val")
        End If
    End Sub
    
    Protected Sub DL_flattivo_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        ' Resetta indice di selezione sulle dropdwonlist per non perderlo a seguito passaggio a pagina di dettaglio
        If Not IsPostBack And Session("Persons_DL_flattivo_val") <> Nothing Then
            DL_flattivo.SelectedValue = Session("Persons_DL_flattivo_val")
        End If
    End Sub
    
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Lista persone</title>
    <link href="/timereport/include/commoncss.aspx" rel="stylesheet" type="text/css" />
</head>

<script language="JavaScript" src="/timereport/include/menu/menu_array.js" type="text/javascript"></script>

<script language="JavaScript" src="/timereport/include/menu/mmenu.js" type="text/javascript"></script>

<body>
    <table width="760" border="0">
        <tr>
            <td height="30" class="BarraTop" align="right">
                Utente:
                <%= session("UserName") %>
            </td>
    </table>
    <br />
    <form id="form1" runat="server">
    <table width="760" border="0" class="GridTab">
        </tr>
        <td>
            Attivo:
        </td>
        <td>
            <asp:DropDownList ID="DL_flattivo" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DL_flattivo_SelectedIndexChanged"
                OnPreRender="DL_flattivo_Load" CssClass="TabellaLista">
                <asp:ListItem Value="all">Tutti i valori</asp:ListItem>
                <asp:ListItem Selected="True" Value="1">Attivo</asp:ListItem>
                <asp:ListItem Value="0">Non attivo</asp:ListItem>
            </asp:DropDownList>
        </td>
        <td>
            Nome:
        </td>
        <td>
            <asp:TextBox ID="TB_Nome" runat="server" OnTextChanged="TB_Codice_TextChanged" OnLoad="TB_Nome_Load"
                CssClass="TabellaLista"></asp:TextBox>
            &nbsp;<asp:Button ID="Button1" runat="server" Text="filtra" />
        </td>
        </tr>
    </table>
    <asp:SqlDataSource ID="DS_Persone" runat="server" ConnectionString="<%$ ConnectionStrings:MSSql12155ConnectionString %>"
        SelectCommand="SELECT DISTINCT Persons.Persons_id, Persons.Name FROM Persons INNER JOIN Projects ON Persons.Persons_id = Projects.ClientManager_id WHERE (Persons.Active = @Active) ORDER BY Persons.Name">
        <SelectParameters>
            <asp:Parameter DefaultValue="true" Name="Active" Type="Boolean" />
        </SelectParameters>
    </asp:SqlDataSource>
    <asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
        DataSourceID="PersonsLookUpSqlDataSource" CssClass="GridView" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
        AllowPaging="True" PageSize="15" DataKeyNames="Persons_id, Nome" GridLines="None">
        <FooterStyle CssClass="GV_footer" />
        <RowStyle Wrap="False" CssClass="GV_row" />
        <Columns>
            <asp:BoundField DataField="Nome" HeaderText="Nome" SortExpression="Nome" />
            <asp:BoundField DataField="Ruolo" HeaderText="Ruolo" SortExpression="Ruolo" />
            <asp:CommandField ShowDeleteButton="True" ShowSelectButton="True" ButtonType="Image"
                DeleteImageUrl="/timereport/images/icons/16x16/trash.gif" SelectImageUrl="/timereport/images/icons/16x16/modifica.gif" />
        </Columns>
        <PagerStyle CssClass="GV_footer" />
        <HeaderStyle CssClass="GV_header" />
        <AlternatingRowStyle CssClass="GV_row_alt " />
    </asp:GridView>
    <asp:SqlDataSource ID="PersonsLookUpSqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:MSSql12155ConnectionString %>"
        DeleteCommand="DELETE FROM [Persons] WHERE [Persons_id] = @Persons_id">
        <SelectParameters>
            <asp:ControlParameter ControlID="DL_flattivo" Name="DL_flattivo" PropertyName="SelectedValue"
                Type="String" />
            <asp:ControlParameter ControlID="TB_Nome" Name="TB_Nome" PropertyName="text" DefaultValue="%" />
        </SelectParameters>
        <DeleteParameters>
            <asp:Parameter Name="Persons_id" Type="string" />
        </DeleteParameters>
    </asp:SqlDataSource>
    <asp:Button ID="btn_crea" runat="server" Text="Nuovo record" CssClass="Gridbutton"
        PostBackUrl="/timereport/lookup_detail.asp?insert=Nuovo+record" />
    <asp:Button ID="btn_back" runat="server" Text="Annulla" CssClass="Gridbutton" PostBackUrl="/timereport/menu.aspx" />
    </form>
</body>
</html>
